package cn.mcch.investigator.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.TextView;

import cn.mcch.investigator.R;

public class StateEditText extends EditText {

	private StateListDrawable mStateBackground;

	private int mNormalBackgroundColor;
	private int mPressedBackgroundColor;
	private int mDisableBackgroundColor;

	private GradientDrawable mNormalBackground;
	private GradientDrawable mPressedBackground;
	private GradientDrawable mDisableBackground;

	private float mRadius;
	private boolean mRound;

	private int mStrokWidth;

	private int mStrokeColor;

	public StateEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public StateEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}

	public StateEditText(Context context) {
		super(context);
	}

	@SuppressLint("NewApi")
	private void init(AttributeSet attrs) {
		Drawable drawable = getBackground();
		if (drawable != null && drawable instanceof StateListDrawable) {
			mStateBackground = (StateListDrawable) drawable;
		} else {
			mStateBackground = new StateListDrawable();
		}

		TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.StateButton);
		// 圆角
		mRadius = a.getDimensionPixelSize(R.styleable.StateButton_radius, 0);
		mRound = a.getBoolean(R.styleable.StateButton_round, false);
		mRadius = mRound ? (Integer.MAX_VALUE) : mRadius;
		// set stroke
		mStrokWidth = a.getDimensionPixelSize(R.styleable.StateButton_strokeWidth, 0);
		mStrokeColor = a.getColor(R.styleable.StateButton_strokeColor, 0);

		// 颜色
		mNormalBackgroundColor = a.getColor(R.styleable.StateButton_normalBackgroundColor, 0);
		if (mNormalBackgroundColor != 0) {
			mNormalBackground = new GradientDrawable();
			mNormalBackground.setColor(mNormalBackgroundColor);
			if (mRadius > 0) {
				mNormalBackground.setCornerRadius(mRadius);
			}
			if (mStrokWidth > 0) {
				mNormalBackground.setStroke(mStrokWidth, mStrokeColor);

			}
		}

		mPressedBackgroundColor = a.getColor(R.styleable.StateButton_pressedBackgroundColor, 0);
		if (mPressedBackgroundColor != 0) {
			mPressedBackground = new GradientDrawable();
			mPressedBackground.setColor(mPressedBackgroundColor);
			if (mRadius > 0) {
				mPressedBackground.setCornerRadius(mRadius);
			}
			if (mStrokWidth > 0) {

				mPressedBackground.setStroke(mStrokWidth, mStrokeColor);

			}
		}

		mDisableBackgroundColor = a.getColor(R.styleable.StateButton_disableBackgroundColor, 0);
		if (mDisableBackgroundColor != 0) {
			mDisableBackground = new GradientDrawable();
			mDisableBackground.setColor(mDisableBackgroundColor);
			if (mRadius > 0) {
				mDisableBackground.setCornerRadius(mRadius);
			}
			if (mStrokWidth > 0) {
				mDisableBackground.setStroke(mStrokWidth, mStrokeColor);
			}
		}

		// drawable.addState的第一个参数虽然是一个整型数组，但是经过测试发现如果有多个值存在，
		// stateListDrawable似乎就失效了，故使用时，数组里放单一属性就可以了。
		if (mPressedBackground != null) {
			mStateBackground.addState(new int[] { android.R.attr.state_checked }, mPressedBackground);
			mStateBackground.addState(new int[] { android.R.attr.state_pressed }, mPressedBackground);
		}
		if (mDisableBackground != null) {
			mStateBackground.addState(new int[] { -android.R.attr.state_enabled }, mDisableBackground);
		}
		// 没有任何状态时显示的图片，我们给它设置我空集合
		if (mNormalBackground != null) {
			mStateBackground.addState(new int[] {}, mNormalBackground);
		}
		this.setBackground(mStateBackground);
		a.recycle();

	}

}
